package admin

import (
	"object/go_cms/models"
)

type AdminAdminController struct {
	BaseAdminController
	models models.AdminModels
}

// @router /admin/admin/indexUI [get]
func (c *AdminAdminController) IndexUI() {
	c.TplAdmin("admin/index")
}

// @router /admin/admin/addUI [get]
func (c *AdminAdminController) AddUI() {
	c.TplAdmin("admin/add")
}

// @router /admin/admin/editUI [get]
func (c *AdminAdminController) EditUI() {
	c.TplAdmin("admin/edit")
}

// 获取账号
// @router /admin/admin/get [get,post]
func (c *AdminAdminController) Get() {
	c.Res.Code = 1
	c.Res.Msg = ""
	id, err := c.GetInt64("id")
	if err != nil {
		c.Res.Code = 0
		c.Res.Msg = "参数有误"
	} else {
		admin, err := c.models.GetAdmin(id)
		g := models.AdminGroupModels{}
		agm, _ := g.Query(id)
		mapc := make(map[string]interface{})
		mapc["admin"] = admin
		mapc["group"] = agm
		c.Res.Data = mapc
		if err != nil {
			c.Res.Code = 0
			c.Res.Msg = err.Error()
		}
	}
	c.Json(c.Res)
}

// 列表
// @router /admin/admin/query [get,post]
func (c *AdminAdminController) QueryByPage() {
	maps := c.GetParamMap()
	c.Res.Code = 0
	c.Res.Data, c.Res.Count = c.models.QueryByPageAdmin(maps)
	c.Json(c.Res)
}

// @router /admin/admin/add [post]
func (c *AdminAdminController) Add() {
	maps := c.GetParamMap()
	c.Res.Code = 1
	c.Res.Msg = "添加成功"
	row, err := c.models.InsertAdmin(maps)
	if err != nil {
		c.Res.Code = 0
		c.Res.Msg = err.Error()
	} else {
		c.Res.Data = row
	}
	c.Json(c.Res)
}

// @router /admin/admin/edit [post]
func (c *AdminAdminController) Edit() {
	maps := c.GetParamMap()
	c.Res.Code = 1
	c.Res.Msg = "修改成功"
	_, err := c.models.UpdateAdmin(maps)
	if err != nil {
		c.Res.Code = 0
		c.Res.Msg = err.Error()
	}
	c.Json(c.Res)
}

// @router /admin/admin/delete [post]
func (c *AdminAdminController) Delete() {
	c.Res.Code = 1
	c.Res.Msg = "删除成功"
	id, _ := c.GetInt64("id")
	_, err := c.models.DeleteAdmin(id)
	if err != nil {
		c.Res.Code = 0
		c.Res.Msg = err.Error()
	}
	c.Json(c.Res)
}
